// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import Vuex from 'vuex'
import store from './vuex/store'
Vue.use(Vuex)

import VueCookies from 'vue-cookies'
Vue.use(VueCookies)

import Vant from 'vant';
import { Toast } from 'vant';
import 'vant/lib/index.css';
Vue.use(Vant);

import AMap from 'vue-amap';
Vue.use(AMap);

import $ from 'jquery';


// 初始化vue-amap
AMap.initAMapApiLoader({
    // 高德key
    key: '657b30f6ee334d9dd973450ef17f8d70',
    // 插件集合 （插件按需引入）
    plugin: ['AMap.Geolocation']
});


import vuefilter from "./common/vuefilter"
import JsBridge from "./common/JsBridge"
import api from "./common/api"
import $axios from "./common/axios"

Vue.prototype.$api = api;
Vue.prototype.$axios = $axios;
Vue.prototype.$vuefilter = vuefilter;
Vue.prototype.$JsBridge = new JsBridge();
Vue.config.productionTip = false



Vue.directive('anchor', {
    inserted: function(el, binding) {
        el.onclick = function() {
            document.documentElement.scrollTop = $('#anchor-' + binding.value).offset().top
        }
    }
})

/* eslint-disable no-new */
new Vue({
    el: '#app',
    router,
    store,
    components: { App },
    template: '<App/>'
})

//通过路由拦截，判断个别页面是否需要token
router.beforeEach((to, from, next) => {

    if (to.meta.requireAuth) { // 判断该路由是否需要登录权限
        var token = localStorage.getItem("token");

        if (token) { // 通过vuex state获取当前的token是否存在
            next();
        } else {
            localStorage.setItem('beforeLoginUrl', to.fullPath);

            next({
                path: '/login',
                query: { redirect: to.fullPath } // 将跳转的路由path作为参数，登录成功后跳转到该路由
            })
        }
    } else {
        next();
    }
});